﻿using System;
using System.Collections.Generic;
using System.Linq; 
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using IDML.BusinessObject;
using IDML.BusinessLogic;

namespace IDML.Web.Modules.Patient
{
    public partial class View : System.Web.UI.Page
    {
        PatientBO _patientBo;
        PatientBL _patientBl;

        DataTable dtPayDetails;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["PATID"] != null )
            {
                int patID = Convert.ToInt32(Request.QueryString["PATID"]);
                LoadPatientDetails(patID);
            }
           
        }

        private void LoadPatientDetails(int patID)
        {
            _patientBo = new PatientBO();
            _patientBl = new PatientBL();
            _patientBo.PatientId = patID;

             List<PatientBO> patientList = _patientBl.Patient_LoadDetails(patID);

             lblFirstName.Text = patientList[0].FirstName;
             lblLastName.Text = patientList[0].LastName;
             lblPhoneNumber.Text = patientList[0].PhoneNumber;
             lblMobileNumber.Text = patientList[0].Age.ToString();
        }


        protected void btnAddTestPay_Click(object sender, EventArgs e)
        {
            TestBO _testBO = new TestBO();
            TestBL _testBL = new TestBL();

            dtPayDetails = new DataTable();

            _testBO.TestCode = txtTestCode.Text;
            List<TestFeeStructDetailsBO> testFeeStructDet = _testBL.GetTestFeeStructure(_testBO);


            if (ViewState["PayDetails"] == null)
            {
                dtPayDetails.Columns.Add("TestId", typeof(int));
                dtPayDetails.Columns.Add("TestFeeStructureId", typeof(int));
                dtPayDetails.Columns.Add("TestCode", typeof(string));
                dtPayDetails.Columns.Add("TestName", typeof(string));
                dtPayDetails.Columns.Add("Amount", typeof(decimal));
            }
            else
            {
                dtPayDetails = (DataTable)ViewState["PayDetails"];
            }

            dtPayDetails.Rows.Add(testFeeStructDet[0].TestId, testFeeStructDet[0].TestFeeStructureId, testFeeStructDet[0].TestCode, testFeeStructDet[0].TestName, testFeeStructDet[0].Amount);
            ViewState["PayDetails"] = dtPayDetails;

            gwPaymentView.DataSource = ViewState["PayDetails"];
            gwPaymentView.DataBind();
        }

        protected void gwPaymentView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int testID = Convert.ToInt32(gwPaymentView.DataKeys[e.RowIndex].Values["TestId"]);
            DeleteAccompanyingData(testID);
            dtPayDetails = (DataTable)ViewState["PayDetails"];

            gwPaymentView.DataSource = dtPayDetails;
            gwPaymentView.DataBind();
        }

        private void DeleteAccompanyingData(int testID)
        {
            dtPayDetails = new DataTable();
            dtPayDetails = (DataTable)ViewState["PayDetails"];

            for (int i = dtPayDetails.Rows.Count - 1; i >= 0; i--)
            {
                DataRow dr = dtPayDetails.Rows[i];
                if (Convert.ToInt32(dr["TestId"]) == testID)
                    dr.Delete();
            }

            ViewState["PayDetails"] = dtPayDetails; 
        }

    }
}